//------------------------------------------------------------------------------------------------- 
// <copyright file="SPEmailHelper.cs" company="Collaboris">
// Copyright (c) Collaboris.  All rights reserved.
// </copyright>
// <summary>Email Helper Library.</summary>
//-------------------------------------------------------------------------------------------------
namespace Collaboris.SharePoint.Wss.Administraton
{
    using System;
    using System.Collections.Specialized;

    using Collaboris.Utils;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;

    /// <summary>
    /// Adds helper methods to the Sharepoint <see cref="SPFile"/> class.
    /// </summary>
    public class SPEmailHelper : WssHelperBase
    {
        #region [rgn] Methods (1)

        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="spWeb">The sp web.</param>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="cc">The cc.</param>
        /// <param name="bcc">The BCC.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="isBodyHTML">if set to <c>true</c> [is body HTML].</param>
        /// <returns></returns>
        public static string SendEmail(
                SPWeb spWeb,
                string from,
                string to,
                string cc,
                string bcc,
                string subject,
                string body,
                bool isBodyHTML)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "SPEmailHelper", "SendEmail");
            
            string emailSummary;

            StringDictionary messageHeaders = new StringDictionary();

            ValidationHelper.VerifyStringArgument(to, "to");
            ValidationHelper.VerifyStringArgument(from, "from");
            ValidationHelper.VerifyStringArgument(subject, "subject");
            ValidationHelper.VerifyStringArgument(cc, "cc");
            ValidationHelper.VerifyStringArgument(bcc, "bcc");
            
            messageHeaders.Add("to", to);
            messageHeaders.Add("from", from);
            messageHeaders.Add("subject", subject);
            messageHeaders.Add("cc", cc);
            messageHeaders.Add("bcc", bcc);
        
            string mimeType = "text/plain";
            if (isBodyHTML)
            {
                mimeType = "text/html";
            }

            messageHeaders.Add("content-type", mimeType);

            bool sendMail = SPUtility.SendEmail(
                    spWeb,
                    messageHeaders,
                    body);

            if (sendMail)
            {
                emailSummary = "<EmailMessage>" +
                               "<To>" + to + "</To>" +
                               "<From>" + from + "</From>" +
                               "<Subject>" + SPEncode.HtmlEncode(subject) + "</Subject>" +
                               "<CC>" + cc + "</CC>" +
                               "<BCC>" + bcc + "</BCC>" +
                               "<Body>" + SPEncode.HtmlEncode(body) + "</Body>" +
                               "</EmailMesage>";
            }
            else
            {
                throw new ApplicationException("Failed to send email.");
            }

            TraceHelper.TraceMethodEnd(TraceSwitch, "SPEmailHelper", "SendEmail");
            return emailSummary;
        }

        #endregion
    }
}